package com.lottery.im.module.service.presenter;

import android.app.Activity;
import android.util.Log;
import com.lottery.im.MyApplication;
import com.lottery.im.api.Api;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.BaseListObserver;
import com.lottery.im.api.observer.BaseObserver;
import com.lottery.im.api.observer.ShowTipListObserver;
import com.lottery.im.api.result.ApiListResult;
import com.lottery.im.api.result.entity.integral.ExchangeLog;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.service.view.ExchangeLogView;
import com.lottery.im.util.ToastUtils;
import com.lottery.im.widget.dialog.LoadingDialog;
import com.lottery.im.widget.dialog.LoadingDialogUtil;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 作者：cyy
 * 时间：2018/9/20。
 */
public class ExchangeLogPresenter extends BaseFragmentPresenter<ExchangeLogView> {
  public ExchangeLogPresenter(Activity activity,
      ExchangeLogView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  String userId = MyApplication.getInstance().getUserInfo().getUserId();
  String token = MyApplication.getInstance().getToken();

  public void loadExchangeLogGetLimit(int startRow,int pageSize,int state){
    ApiClient.getApiRetrofitInstance()
        .exchangeLogGetLimit(userId,token,startRow,pageSize)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseListObserver<ExchangeLog>() {
          @Override public void onStart() {
            if(state==0){//第一次进来
              showLoadingProgress("正在加载");
            }
          }

          @Override public void onSuccess(List<ExchangeLog> t) {
            iView.loadExchangeLogGetLimitSuccess(t,state);
            closeLoadingProgress();
          }

          @Override public void onFail(String code, String msg) {
            ToastUtils.showToast(activity,msg);
            closeLoadingProgress();
          }

          @Override public void onException(String msg) {
            ToastUtils.showToast(activity,msg);
            closeLoadingProgress();
          }

          @Override public void onFinish() {
            closeLoadingProgress();
          }
        });
    }
}
